package com.example.book.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class TimeAspect {

    @Around("execution(* com.example.book.controller.*.*(..))")
    public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {
        //方法执行前时间戳
        long begin = System.currentTimeMillis();

        //执行方法
        Object result = joinPoint.proceed();

        //方法执行后时间戳
        long end = System.currentTimeMillis();

        //方法执行耗时
        log.info("执行时间：{}", end - begin + " ms");

        return result;
    }
}
